package com.verisette.ncos.service.card.bizunit;

import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.corp.agi.common.exception.ProcessException;
import org.corp.agi.service.bean.ProcessContext;
import org.corp.agi.service.bizunit.CommonBusinessUnit;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
import com.verisette.ncos.common.ProjectConstant;
import com.verisette.ncos.persistence.domain.MasterMember;
import com.verisette.ncos.persistence.domain.MasterTemplate;
import com.verisette.ncos.persistence.domain.MasterUserProfile;
import com.verisette.ncos.persistence.domain.TransCustomerOrder;

public class CreateMittarePurchaseOrderBU  extends CommonBusinessUnit {
	Logger logger = Logger.getLogger(CreateMittarePurchaseOrderBU.class);
	
	private boolean isTest = false;

	public CreateMittarePurchaseOrderBU(){
		super();
	}
	
	public CreateMittarePurchaseOrderBU(boolean isTest){
		super();
		this.isTest = isTest;
	}
	
	public void execute(ProcessContext processContext) throws ProcessException {
		if(logger.isInfoEnabled()) logger.info( this.getClass().getSimpleName() + ": execute" );

		TransCustomerOrder order = (TransCustomerOrder)processContext.getValueObject();
		
		if(!isTest){
			order = (TransCustomerOrder) commonDAO.findById(
						TransCustomerOrder.class, order.getOrderId());
		}

		MasterMember member = order.getMasterMember();
		MasterTemplate template = order.getMasterTemplate();
		
		if(logger.isInfoEnabled()) logger.info( this.getClass().getSimpleName() + ": execute next" );
		
		MasterUserProfile approver2 = (MasterUserProfile)processContext.getUserBean();
		String userPDFPathDir = ProjectConstant.APP_ORDER_PO_FOLDER;
//		String fullPDFPathFile = userPDFPathDir+"\\"+(new SimpleDateFormat("'PO_'ddMMyyyy'_'HHmmss'_'SSS", ProjectConstant.defaultLocale)).format(new Date())+".pdf";
		String fullPDFPathFile = userPDFPathDir+"\\"+order.getOrderNoDisp()+".pdf";
		Document document = new Document(PageSize.A4, 20f, 20f, 20f, 20f);
		PdfWriter writer = null;
		
		File output=null;
		
		try{
			MasterUserProfile userBean = (MasterUserProfile) processContext.getUserBean();
			final String companyName = userBean.getMasterCompany().getCompanyNameTh();
			
			File dir = new File(userPDFPathDir);
			if(!dir.exists()) dir.mkdirs();
			
			output = new File(fullPDFPathFile);
			writer = PdfWriter.getInstance(document, new FileOutputStream(output));
			
			FontFactory.register(ProjectConstant.FONT_PSLPRO_NORMAL, ProjectConstant.FONT_NAME_PSLPRO_NORMAL);
			FontFactory.register(ProjectConstant.FONT_PSLPRO_BOLD, ProjectConstant.FONT_NAME_PSLPRO_BOLD);
			FontFactory.register(ProjectConstant.FONT_PSLPRO_ITALIC, ProjectConstant.FONT_NAME_PSLPRO_ITALIC);
			
			System.out.println("PageSize {" + PageSize.A4.getWidth() + "," + PageSize.A4.getHeight() + "}");
			
	        document.open();
	        
	        Image title = Image.getInstance(String.format(ProjectConstant.APP_IMAGES_FOLDER + "\\BG\\OrderTitle.jpg"));
	        
	        title.scalePercent(31.5f);
	        document.add(title);
	        
    		BaseFont font = BaseFont.createFont(ProjectConstant.FONT_PSLPRO_NORMAL, ProjectConstant.EXPORT_ENCODE_TIS620, BaseFont.EMBEDDED);
    		BaseFont fontBold = BaseFont.createFont(ProjectConstant.FONT_PSLPRO_BOLD, ProjectConstant.EXPORT_ENCODE_TIS620, BaseFont.EMBEDDED);
    		Font fontBoldUnderlineUp = FontFactory.getFont(ProjectConstant.FONT_PSLPRO_BOLD, ProjectConstant.EXPORT_ENCODE_TIS620, BaseFont.EMBEDDED, 15, Font.UNDERLINE);
    		BaseFont fontBoldUnderline = fontBoldUnderlineUp.getBaseFont();
    		
    		PdfContentByte over = writer.getDirectContent();
    		over.saveState();                         
	        over.beginText();
    		
	        //OrderTitle #################################################
    		Rectangle recOrderTitle = new Rectangle(30, 740, 292.5f, 690);
    		recOrderTitle.setBorder(Image.BOX);
    		recOrderTitle.setBorderWidth(0.5f);
    		recOrderTitle.setBorderColor(BaseColor.BLACK);
	        document.add(recOrderTitle);
	        
	        over.setFontAndSize(fontBold,20);    		
    		over.showTextAligned(Element.ALIGN_CENTER, "ใบสั่งซื้อ", 166.25f, 720, 0);
    		over.showTextAligned(Element.ALIGN_CENTER, "Purchase Order", 166.25f, 700, 0);
    		
    		//OrderNo #################################################
    		Rectangle recOrderNo = new Rectangle(297.5f, 740, 428.75f, 690);
    		recOrderNo.setBorder(Image.BOX);
    		recOrderNo.setBorderWidth(0.5f);
    		recOrderNo.setBorderColor(BaseColor.BLACK);
	        document.add(recOrderNo);
    		over.setFontAndSize(fontBold,16);    		
    		over.showTextAligned(Element.ALIGN_LEFT, "เลขที่ใบสั่งซื้อ:", 305.5f, 720, 0);
    		over.setFontAndSize(font,16);    	
    		over.showTextAligned(Element.ALIGN_LEFT, order.getOrderNoDisp(), 305.5f, 700, 0);
    		
    		//OrderDate #################################################
    		Rectangle recOrderDate = new Rectangle(433.75f, 740, 565, 690);
    		recOrderDate.setBorder(Image.BOX);
    		recOrderDate.setBorderWidth(0.5f);
    		recOrderDate.setBorderColor(BaseColor.BLACK);
	        document.add(recOrderDate);
    		over.setFontAndSize(fontBold,16);    	
    		over.showTextAligned(Element.ALIGN_LEFT, "วันที่ :", 441.75f, 720, 0);
    		over.setFontAndSize(font,16);    	
    		over.showTextAligned(Element.ALIGN_LEFT, order.getCreatedDateDisp(), 441.75f, 700, 0);
    		
    		Rectangle rec = null;
    		ColumnText ct = null;
    		
    		final String headerName = "ชื่อ-สกุล";
    		final String headerCompany = "บริษัท";
    		final String headerAddr = "ที่อยู่";
    		final String delimiter = ":";
    		
    		//Customer profile #################################################
    		rec = new Rectangle(30, 680, 292.5f, 660);
    		rec.setBorderColor(BaseColor.BLACK);
    		rec.setBackgroundColor(BaseColor.LIGHT_GRAY);
	        document.add(rec);
	        
    		rec = new Rectangle(30, 680, 292.5f, 570);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);

    		over.setFontAndSize(fontBold,16);    	
    		over.showTextAligned(Element.ALIGN_LEFT, "รายละเอียดลูกค้า", 32, 665, 0);
    		
    		over.showTextAligned(Element.ALIGN_LEFT, headerName, 32, 645, 0);
    		over.showTextAligned(Element.ALIGN_LEFT, delimiter, 67, 645, 0);
    		
    		over.setFontAndSize(font,16);    	
    		over.showTextAligned(Element.ALIGN_LEFT, order.getMemberFullNameDisp() , 72, 645, 0);

    		over.setFontAndSize(fontBold,16);
    		over.showTextAligned(Element.ALIGN_LEFT, headerCompany, 32, 625, 0);
    		over.showTextAligned(Element.ALIGN_LEFT, delimiter, 67, 625, 0);
    		
    		over.setFontAndSize(font,16);
    		over.showTextAligned(Element.ALIGN_LEFT, companyName, 72, 625, 0);

    		over.setFontAndSize(fontBold,16);
    		over.showTextAligned(Element.ALIGN_LEFT, headerAddr, 32, 605, 0);
    		over.showTextAligned(Element.ALIGN_LEFT, delimiter, 67, 605, 0);
    		
    		over.saveState();
    		ct = new ColumnText(over);
    		ct.setSimpleColumn(new Phrase(new Chunk(
    				order.getMemberAddrDisp(), 
    				new Font(font, 16))), 
    				72, 625, 292.5f, 570, 
    				20, Element.ALIGN_LEFT);
    		ct.go();
    		over.restoreState();
    		
    		//Delivery profile #################################################
    		rec = new Rectangle(297.5f, 680, 565, 660);
    		rec.setBorderColor(BaseColor.BLACK);
    		rec.setBackgroundColor(BaseColor.LIGHT_GRAY);
	        document.add(rec);
	        
    		rec = new Rectangle(297.5f, 680, 565, 570);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);

    		over.setFontAndSize(fontBold,16);
    		over.showTextAligned(Element.ALIGN_LEFT, "รายละเอียดการจัดส่งสินค้า", 299.5f, 665, 0);
    		
    		over.showTextAligned(Element.ALIGN_LEFT, headerName, 299.5f, 645, 0);
    		over.showTextAligned(Element.ALIGN_LEFT, delimiter, 334.5f, 645, 0);
    		
    		over.setFontAndSize(font,16);
    		over.showTextAligned(Element.ALIGN_LEFT, order.getDeliveryName(), 339.5f, 645, 0);

    		over.setFontAndSize(fontBold,16);
    		over.showTextAligned(Element.ALIGN_LEFT, headerCompany, 299.5f, 625, 0);
    		over.showTextAligned(Element.ALIGN_LEFT, delimiter, 334.5f, 625, 0);
    		
    		if(StringUtils.isNotBlank(order.getDeliveryName())){
	    		over.setFontAndSize(font,16);
	    		over.showTextAligned(Element.ALIGN_LEFT, companyName, 339.5f, 625, 0);
    		}
    		
    		over.setFontAndSize(fontBold,16);
    		over.showTextAligned(Element.ALIGN_LEFT, headerAddr, 299.5f, 605, 0);
    		over.showTextAligned(Element.ALIGN_LEFT, delimiter, 334.5f, 605, 0);
    		
    		over.saveState();
    		ct = new ColumnText(over);
    		ct.setSimpleColumn(new Phrase(new Chunk(
    				order.getDeliveryAddrDisp(),
    				new Font(font, 16))), 
    				339.5f, 625, 565, 570, 
    				20, Element.ALIGN_LEFT);
    		ct.go();
    		over.restoreState();
    		
    		//##################################################################
    		//##################################################################
	        //Product profile list header #################################################
    		float boxh = 20;
    		float posy1 = 560;
    		float posy2 = posy1 - boxh;
    		float textposy = getTextPosY(posy1, posy2);
    		
    		final float commonboxw = 50;
    		final float detailboxw = 235;
    		final float boxstart = 30f;
    		final float boxTotalWidth = 535f;
    		
    		float boxposx1 = boxstart;
    		float boxposx2 = boxposx1 + commonboxw;
    		float textposx = getTextPosX(boxposx1, boxposx2,Element.ALIGN_CENTER);
    		
    		//Product Order
    		rec = new Rectangle(boxposx1, posy1, boxposx2, posy2);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
    		rec.setBackgroundColor(BaseColor.LIGHT_GRAY);
	        document.add(rec);
    		over.showTextAligned(Element.ALIGN_CENTER, "ลำดับที่", textposx, textposy, 0);
    		
    		boxposx1 = boxposx2;
    		boxposx2 = boxposx1 + commonboxw;
    		textposx = getTextPosX(boxposx1, boxposx2,Element.ALIGN_CENTER);
	        //Product Code header
    		rec = new Rectangle(boxposx1, posy1, boxposx2, posy2);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
    		rec.setBackgroundColor(BaseColor.LIGHT_GRAY);
	        document.add(rec);
    		over.showTextAligned(Element.ALIGN_CENTER, "รหัสสินค้า", textposx, textposy, 0);

    		boxposx1 = boxposx2;
    		boxposx2 = boxposx1 + detailboxw;
    		textposx = getTextPosX(boxposx1, boxposx2,Element.ALIGN_CENTER);
	        //Order detail header
    		rec = new Rectangle(boxposx1, posy1, boxposx2, posy2);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
    		rec.setBackgroundColor(BaseColor.LIGHT_GRAY);
	        document.add(rec);
    		over.showTextAligned(Element.ALIGN_CENTER, "รายการ", textposx, textposy, 0);

    		boxposx1 = boxposx2;
    		boxposx2 = boxposx1 + commonboxw;
    		textposx = getTextPosX(boxposx1, boxposx2,Element.ALIGN_CENTER);
	        //Unit header
    		rec = new Rectangle(boxposx1, posy1, boxposx2, posy2);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
    		rec.setBackgroundColor(BaseColor.LIGHT_GRAY);
	        document.add(rec);
    		over.showTextAligned(Element.ALIGN_CENTER, "หน่วย", textposx, textposy, 0);

    		textposy = getTextPosY(posy1, posy2);
    		boxposx1 = boxposx2;
    		boxposx2 = boxposx1 + commonboxw;
    		textposx = getTextPosX(boxposx1, boxposx2,Element.ALIGN_CENTER);
	        //Amount header
    		rec = new Rectangle(boxposx1, posy1, boxposx2, posy2);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
    		rec.setBackgroundColor(BaseColor.LIGHT_GRAY);
	        document.add(rec);
    		over.showTextAligned(Element.ALIGN_CENTER, "จำนวน", textposx, textposy, 0);

    		textposy = getTextPosY(posy1, posy2);
    		boxposx1 = boxposx2;
    		boxposx2 = boxposx1 + commonboxw;
    		textposx = getTextPosX(boxposx1, boxposx2,Element.ALIGN_CENTER);
	        //Price per unit header
    		rec = new Rectangle(boxposx1, posy1, boxposx2, posy2);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
    		rec.setBackgroundColor(BaseColor.LIGHT_GRAY);
	        document.add(rec);
    		over.showTextAligned(Element.ALIGN_CENTER, "หน่วยละ", textposx, textposy, 0);

    		textposy = getTextPosY(posy1, posy2);
    		boxposx1 = boxposx2;
    		boxposx2 = boxposx1 + commonboxw;
    		textposx = getTextPosX(boxposx1, boxposx2,Element.ALIGN_CENTER);
	        //price header
    		rec = new Rectangle(boxposx1, posy1, boxposx2, posy2);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
    		rec.setBackgroundColor(BaseColor.LIGHT_GRAY);
	        document.add(rec);
    		over.showTextAligned(Element.ALIGN_CENTER, "จำนวนเงิน", textposx, textposy, 0);

    		//##################################################################
    		//##################################################################
	        //Product profile list detail ##################################################
    		over.setFontAndSize(font,16);
    		
    		boxh = 60;
    		
    		posy1 = posy2;
    		posy2 = posy1 - boxh;
    		textposy = getTextPosY(posy1, posy2);
    		boxposx1 = boxstart;
    		boxposx2 = boxposx1 + commonboxw;
    		textposx = getTextPosX(boxposx1, boxposx2,Element.ALIGN_CENTER);
    		 //Product Order #2
    		rec = new Rectangle(boxposx1, posy1, boxposx2, posy2);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
    		over.showTextAligned(Element.ALIGN_CENTER, "1",  textposx, textposy, 0);
    		
    		boxposx1 = boxposx2;
    		boxposx2 = boxposx1 + commonboxw;
    		textposx = getTextPosX(boxposx1, boxposx2,Element.ALIGN_CENTER);
	        //Product Code #2
    		rec = new Rectangle(boxposx1, posy1, boxposx2, posy2);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
    		over.showTextAligned(Element.ALIGN_CENTER, order.getProductCodeDisp(), textposx, textposy, 0);

    		boxposx1 = boxposx2;
    		boxposx2 = boxposx1 + detailboxw;
    		textposx = getTextPosX(boxposx1, boxposx2,Element.ALIGN_LEFT);
    		
	        //Order detail #2
    		rec = new Rectangle(boxposx1, posy1, boxposx2, posy2);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);

    		over.saveState();
    		ct = new ColumnText(over);
    		ct.setSimpleColumn(new Phrase(new Chunk(
    				order.getTemplatePrintDetailDisp(),
    				new Font(font, 16))), 
    				textposx, textposy+20, boxposx2, posy2, 
    				20, Element.ALIGN_LEFT);
    		ct.go();
    		over.restoreState();

    		boxposx1 = boxposx2;
    		boxposx2 = boxposx1 + commonboxw;
    		textposx = getTextPosX(boxposx1, boxposx2,Element.ALIGN_CENTER);
	        //Unit #2
    		rec = new Rectangle(boxposx1, posy1, boxposx2, posy2);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
    		over.showTextAligned(Element.ALIGN_CENTER, "ใบ", textposx, textposy, 0);

    		boxposx1 = boxposx2;
    		boxposx2 = boxposx1 + commonboxw;
    		textposx = getTextPosX(boxposx1, boxposx2,Element.ALIGN_RIGHT);
	        //Amount #2
    		rec = new Rectangle(boxposx1, posy1, boxposx2, posy2);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
    		over.showTextAligned(Element.ALIGN_RIGHT, order.getQtyDisp(), textposx, textposy, 0);

    		boxposx1 = boxposx2;
    		boxposx2 = boxposx1 + commonboxw;
    		textposx = getTextPosX(boxposx1, boxposx2,Element.ALIGN_RIGHT);
	        //Price per unit #2
    		rec = new Rectangle(boxposx1, posy1, boxposx2, posy2);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
    		over.showTextAligned(Element.ALIGN_RIGHT, order.getPricePerUnitDisp(), textposx, textposy, 0);

    		boxposx1 = boxposx2;
    		boxposx2 = boxposx1 + commonboxw;
    		textposx = getTextPosX(boxposx1, boxposx2,Element.ALIGN_RIGHT);
	        //price #2
    		rec = new Rectangle(boxposx1, posy1, boxposx2, posy2);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
    		over.showTextAligned(Element.ALIGN_RIGHT, order.getPriceDisp(), textposx, textposy, 0);
    		
    		//#################################################################
    		//#################################################################
	        //Delivery Fee #########################################################
    		boxh = 20;
    		
    		posy1 = posy2;
    		posy2 = posy1 - boxh;
    		textposy = getTextPosY(posy1, posy2);
    		boxposx1 = boxstart;
    		boxposx2 = boxposx1 + ((commonboxw * 5) + detailboxw);
    		textposx = getTextPosX(boxposx1, boxposx2,Element.ALIGN_RIGHT);
    		rec = new Rectangle(boxposx1, posy1, boxposx2, posy2);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
	        
	        String deliverMethod = String.format("แบบ%s", order.getDeliveryMethodDisp());
	        float deliverMethodLong = fontBold.getWidthPoint(deliverMethod, 16);
	        
    		over.setFontAndSize(font,16);
    		over.showTextAligned(Element.ALIGN_RIGHT, "ค่าจัดส่งสินค้า ", textposx-deliverMethodLong, textposy, 0);
    		over.setFontAndSize(fontBold,16);
    		over.showTextAligned(Element.ALIGN_RIGHT, deliverMethod, textposx, textposy, 0);
    		
    		boxposx1 = boxposx2;
    		boxposx2 = boxposx1 + commonboxw;
    		textposx = getTextPosX(boxposx1, boxposx2,Element.ALIGN_RIGHT);
    		rec = new Rectangle(boxposx1, posy1, boxposx2, posy2);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
    		over.setFontAndSize(font,16);
    		over.showTextAligned(Element.ALIGN_RIGHT, order.getDiliveryCostDisp(), textposx, textposy, 0);
    		

    		//#################################################################
    		//#################################################################
	        //Total Price #########################################################
    		boxh = 30;
    		
    		posy1 = posy2;
    		posy2 = posy1 - boxh;
    		textposy = getTextPosY(posy1, posy2, Element.ALIGN_CENTER);
    		boxposx1 = boxstart;
    		boxposx2 = boxposx1 + ((commonboxw * 5) + detailboxw);
    		textposx = getTextPosX(boxposx1, boxposx2,Element.ALIGN_RIGHT);
    		
    		rec = new Rectangle(boxposx1, posy1, boxposx2, posy2);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
    		over.setFontAndSize(fontBold,18);
    		over.showTextAligned(Element.ALIGN_RIGHT, "รวมจำนวนเงินค่าสินค้าและค่าจัดส่งที่ต้องชำระ", textposx, textposy, 0);
    		
    		boxposx1 = boxposx2;
    		boxposx2 = boxposx1 + commonboxw;
    		textposx = getTextPosX(boxposx1, boxposx2,Element.ALIGN_RIGHT);
    		rec = new Rectangle(boxposx1, posy1, boxposx2, posy2);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
    		over.setFontAndSize(font,18);
    		over.showTextAligned(Element.ALIGN_RIGHT, order.getSumPriceDisp(), textposx, textposy, 0);

    		//#################################################################
    		//#################################################################
	        //Remark #########################################################
    		boxh = 60;
    		
    		posy1 = posy2;
    		posy2 = posy1 - boxh;
    		textposy = getTextPosY(posy1, posy2, Element.ALIGN_TOP);
    		boxposx1 = boxstart;
    		boxposx2 = boxposx1 + (commonboxw * 2);
    		textposx = getTextPosX(boxposx1, boxposx2,Element.ALIGN_LEFT);
    		rec = new Rectangle(boxposx1, posy1, boxposx2, posy2);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
    		over.setFontAndSize(fontBold,16);
    		over.showTextAligned(Element.ALIGN_LEFT, "หมายเหตุ", textposx, textposy, 0);
    		
    		boxposx1 = boxposx2;
    		boxposx2 = boxposx1 + ((commonboxw * 4) + detailboxw);
    		textposx = getTextPosX(boxposx1, boxposx2,Element.ALIGN_LEFT);
    		rec = new Rectangle(boxposx1, posy1, boxposx2, posy2);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
    		over.setFontAndSize(fontBold,16);
    		over.showTextAligned(Element.ALIGN_LEFT, "วิธีชำระเงิน - ", textposx, textposy, 0);
    		over.setFontAndSize(font,16);
    		over.showTextAligned(Element.ALIGN_LEFT, "โอนเงินผ่านบัญชีธนาคาร", textposx+55, textposy, 0);
    		
    		boxh = 15;
    		
    		posy1 = posy2 + 35;
    		posy2 = posy1 -boxh;
    		textposy = getTextPosY(posy1, posy2, Element.ALIGN_TOP);
    		over.setFontAndSize(fontBold,16);
    		over.showTextAligned(Element.ALIGN_LEFT, "สถานที่จัดส่งสินค้า และใบเสร็จรับเงิน", textposx, textposy, 0);

    		posy1 = posy2;
    		posy2 = posy1 - boxh;
    		textposy = getTextPosY(posy1, posy2, Element.ALIGN_TOP);
    		over.setFontAndSize(font,16);
    		over.showTextAligned(Element.ALIGN_LEFT, order.getDeliveryAddrDisp(), textposx, textposy, 0);

    		//#################################################################
    		//#################################################################
	        //Condition ##########################################################
    		boxh = 20;
    		posy1 = posy2 - 10;
    		posy2 = posy1 -boxh;
    		textposy = getTextPosY(posy1, posy2);
    		
    		boxposx1 = boxstart;
    		boxposx2 = boxposx1 + ((commonboxw * 6) + detailboxw);
    		textposx = getTextPosX(boxposx1, boxposx2,Element.ALIGN_LEFT);

    		
	        //Notice the condition #################################################
    		float factorOutGRID = -6.5f;
    		
    		String headerDeliveryCondtion = "เงื่อนไขการจัดส่งสินค้าและบริการ";
    		
    		over.endText();
    		
    		over.setLineWidth(0.3f);
    		over.setColorStroke(BaseColor.BLACK);
    		over.moveTo(textposx+factorOutGRID, textposy-1);
    		over.lineTo(textposx+factorOutGRID+fontBold.getWidthPoint(headerDeliveryCondtion, 16), textposy-1);
    		over.stroke();
    		
    		over.beginText();
    		over.setFontAndSize(fontBoldUnderline,16);
    		over.showTextAligned(Element.ALIGN_LEFT, headerDeliveryCondtion, textposx+factorOutGRID, textposy, 0);
    		over.setFontAndSize(font,16);

    		posy1 = posy2;
    		posy2 = posy1 -boxh;
    		textposy = getTextPosY(posy1, posy2);
    		over.showTextAligned(Element.ALIGN_LEFT, "1. บริษัทฯ จะดำเนินการผลิต เมื่อท่านแจ้งหลักฐานการโอนเงินเรียบร้อยแล้ว ภายใน 7 วันทำการนับจากวันสั่งซื้อสินค้า", textposx+factorOutGRID, textposy, 0);

    		posy1 = posy2;
    		posy2 = posy1 -boxh;
    		textposy = getTextPosY(posy1, posy2);
    		over.showTextAligned(Element.ALIGN_LEFT, "   หากเกินระยะเวลาดังกล่าวใบสั่งซื้อฉบับนี้จะถูกยกเลิก", textposx+factorOutGRID, textposy, 0);

    		posy1 = posy2;
    		posy2 = posy1 -boxh;
    		textposy = getTextPosY(posy1, posy2);
    		over.showTextAligned(Element.ALIGN_LEFT, "2. ใบเสร็จรับเงินจะถูกจัดส่งไปพร้อมกับสินค้า ตามที่อยู่ในการจัดส่งที่ท่านระบุ หรือที่อยู่ตามนามบัตร", textposx+factorOutGRID, textposy, 0);
    		

    		//#################################################################
    		//#################################################################
	        //Adtendee #########################################################
    		boxh = 80;
    		posy1 = posy2;
    		posy2 = posy1 -boxh;
    		textposy = getTextPosY(posy1, posy2);
    		
    		boxposx1 = boxstart;
    		boxposx2 = boxposx1 + ((commonboxw * 6) + detailboxw);
    		textposx = getTextPosX(boxposx1, boxposx2,Element.ALIGN_LEFT);
    		
    		float adtendeeBoxAdj = 40;
    		
    		//User
    		rec = new Rectangle(30, 230+adtendeeBoxAdj, 160, 150+adtendeeBoxAdj);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
	        over.showTextAligned(Element.ALIGN_CENTER, order.getMemberFullNameDisp(), 97, 205+adtendeeBoxAdj, 0);
    		over.setFontAndSize(fontBold,16);
    		over.showTextAligned(Element.ALIGN_CENTER, "ผู้สั่งซื้อ", 97, 175+adtendeeBoxAdj, 0);
    		over.showTextAligned(Element.ALIGN_LEFT, "วันที่", 37, 155+adtendeeBoxAdj, 0);
    		over.setFontAndSize(font,16);
    		over.showTextAligned(Element.ALIGN_CENTER, order.getCreatedDateDisp(), 97, 155+adtendeeBoxAdj, 0);

	        //Printer
    		rec = new Rectangle(165, 230+adtendeeBoxAdj, 295, 150+adtendeeBoxAdj);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
	        over.showTextAligned(Element.ALIGN_CENTER, "พริ้นต์ แอต โพสต์", 232, 205+adtendeeBoxAdj, 0);
    		over.setFontAndSize(fontBold,16);
    		over.showTextAligned(Element.ALIGN_CENTER, "ผู้รับใบสั่งซื้อ", 232, 175+adtendeeBoxAdj, 0);
    		over.showTextAligned(Element.ALIGN_LEFT, "วันที่", 172, 155+adtendeeBoxAdj, 0);
    		over.setFontAndSize(font,16);
    		over.showTextAligned(Element.ALIGN_CENTER, order.getCreatedDateDisp(), 232, 155+adtendeeBoxAdj, 0);

	        //Approver
    		rec = new Rectangle(300, 230+adtendeeBoxAdj, 430, 150+adtendeeBoxAdj);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
    		over.setFontAndSize(fontBold,16);
    		over.showTextAligned(Element.ALIGN_CENTER, "ผู้อนุมัติพิมพ์", 367, 175+adtendeeBoxAdj, 0);
    		over.showTextAligned(Element.ALIGN_LEFT, "วันที่", 307, 155+adtendeeBoxAdj, 0);
    		over.setFontAndSize(font,16);

	        //Receiver
    		rec = new Rectangle(435, 230+adtendeeBoxAdj, 565, 150+adtendeeBoxAdj);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
    		over.setFontAndSize(fontBold,16);
    		over.showTextAligned(Element.ALIGN_CENTER, "ผู้รับสินค้า", 502, 175+adtendeeBoxAdj, 0);
    		over.showTextAligned(Element.ALIGN_LEFT, "วันที่", 442, 155+adtendeeBoxAdj, 0);
    		
    		//#################################################################
    		//#################################################################
	        //Information Footer #####################################################
    		float factorOutGRID2 = 1;
    		boxh = 20;
    		posy1 = posy2;
    		posy2 = posy1 -boxh;
    		
    		boxh = 20;
    		posy1 = posy2;
    		posy2 = posy1 -boxh;
    		textposy = getTextPosY(posy1, posy2);
    		
    		boxposx1 = boxstart;
    		boxposx2 = boxposx1 + (boxTotalWidth/2);
    		textposx = getTextPosX(boxposx1, boxposx2,Element.ALIGN_LEFT);

    		posy1 = posy2;
    		posy2 = posy1 -boxh;
    		textposy = getTextPosY(posy1, posy2);
    		float fontSize = 16;

    		String headerDeliveryMethod = "วิธีการชำระเงินค่าสินค้าและค่าจัดส่ง";
    		over.endText();
    		
    		over.setLineWidth(0.3f);
    		over.setColorStroke(BaseColor.BLACK);
    		over.moveTo(textposx+factorOutGRID, textposy-1);
    		over.lineTo(textposx+factorOutGRID+fontBold.getWidthPoint(headerDeliveryMethod, fontSize), textposy-1);
    		over.stroke();
    		
    		over.beginText();
    		over.setFontAndSize(fontBold,fontSize);
    		over.showTextAligned(Element.ALIGN_LEFT, headerDeliveryMethod, textposx+factorOutGRID, textposy, 0);

    		posy1 = posy2;
    		posy2 = posy1 -boxh;
    		textposy = getTextPosY(posy1, posy2);
    		over.setFontAndSize(font,fontSize);
    		over.showTextAligned(Element.ALIGN_LEFT, "โอนเงินผ่านบัญชีธนาคาร", textposx+factorOutGRID, textposy, 0);

    		posy1 = posy2;
    		posy2 = posy1 -boxh;
    		textposy = getTextPosY(posy1, posy2);
    		boxposx1 = boxstart;
    		boxposx2 = boxposx1 + (boxTotalWidth/2);
    		textposx = getTextPosX(boxposx1, boxposx2,Element.ALIGN_LEFT);
    		
    		over.setFontAndSize(fontBold,fontSize);
    		over.showTextAligned(Element.ALIGN_LEFT, "เลขบัญชี 713-2-51911-5", textposx+factorOutGRID, textposy, 0);

    		boxposx1 = boxposx2;
    		boxposx2 = boxTotalWidth;
    		textposx = getTextPosX(boxposx1, boxposx2,Element.ALIGN_LEFT);
    		over.showTextAligned(Element.ALIGN_LEFT, "หากท่านโอนเงินเรียบร้อยแล้ว กรุณาแฟกซ์หลักฐานการโอนเงิน", textposx+factorOutGRID*factorOutGRID2, textposy, 0);

    		posy1 = posy2;
    		posy2 = posy1 -boxh;
    		textposy = getTextPosY(posy1, posy2);
    		boxposx1 = boxstart;
    		boxposx2 = boxposx1 + (boxTotalWidth/2);
    		textposx = getTextPosX(boxposx1, boxposx2,Element.ALIGN_LEFT);
    		
    		over.setFontAndSize(fontBold,fontSize);
    		over.showTextAligned(Element.ALIGN_LEFT, "ชื่อบัญชี นางสาวสุวรรณี เลิศสุวรรณเสรี", textposx+factorOutGRID, textposy, 0);

    		boxposx1 = boxposx2;
    		boxposx2 = boxTotalWidth;
    		textposx = getTextPosX(boxposx1, boxposx2,Element.ALIGN_LEFT);
    		over.setFontAndSize(fontBold,fontSize);
    		over.showTextAligned(Element.ALIGN_LEFT, "พร้อมระบุวัน-เวลา ยอดเงินโอน และเลขที่ใบสั่งซื้อ มาที่", textposx+factorOutGRID*factorOutGRID2, textposy, 0);

    		posy1 = posy2;
    		posy2 = posy1 -boxh;
    		textposy = getTextPosY(posy1, posy2);
    		boxposx1 = boxstart;
    		boxposx2 = boxposx1 + (boxTotalWidth/2);
    		textposx = getTextPosX(boxposx1, boxposx2,Element.ALIGN_LEFT);
    		
    		over.setFontAndSize(fontBold,fontSize);
    		over.showTextAligned(Element.ALIGN_LEFT, "บัญชีออมทรัพย์ ธนาคากสิกรไทย สาขาถนนพระรามที่ 9", textposx+factorOutGRID, textposy, 0);

    		boxposx1 = boxposx2;
    		boxposx2 = boxTotalWidth;
    		textposx = getTextPosX(boxposx1, boxposx2,Element.ALIGN_LEFT);
    		over.setFontAndSize(fontBold,16);
    		over.showTextAligned(Element.ALIGN_LEFT, "หมายเลขแฟกซ์ 02-719-7579 หรืออีเมล์ print.post99@gmail.com", textposx+factorOutGRID*factorOutGRID2, textposy, 0);
    		
	        over.endText();
	        over.restoreState();        
	        
	        document.close();
		}catch(Exception ex){
			logger.error(ex.getStackTrace());
			ex.printStackTrace();
		}finally{
			if(writer!=null) try{ writer.close(); }catch(Exception ex){ logger.error("writer.close() fail."); }
		}
		
		if(output!=null){
			if(!isTest){
				order = (TransCustomerOrder)commonDAO.findById(TransCustomerOrder.class, order.getOrderId());
				order.setPoPath(output.getName());
				commonDAO.save(order);
			}
		}
		
		processContext.setValueObject(order);
		
		if(logger.isDebugEnabled()){
			logger.debug("fullPDFPathFile::" + fullPDFPathFile);
			logger.debug("PDF (java.io.File)output::" + output);
		}
	}
	
	private float getTextPosX(float posx1, float posx2, int alignment) {
		float textposx = 0;
		if(Element.ALIGN_CENTER == alignment) {
			textposx = ((posx2 - posx1)/2) + posx1;
		} else if(Element.ALIGN_RIGHT == alignment) {
			textposx = posx2 - 5;
		} else if(Element.ALIGN_LEFT == alignment) {
			textposx = posx1 + 10;
		}
		return textposx;
	}

	private float getTextPosY(float posy1, float posy2) {
		return getTextPosY(posy1, posy2, Element.ALIGN_TOP);
	}
	
	private float getTextPosY(float posy1, float posy2, int alignment) {
		float textposy = 0;
		if(Element.ALIGN_TOP== alignment){
			textposy = posy1 - 15;
		} else if(Element.ALIGN_CENTER == alignment) {
			textposy = posy1 - ((posy1 - posy2) / 2) - 5;
		} else if(Element.ALIGN_TOP== alignment){
			textposy = posy2 + 10;
		}
		return textposy;
	}
}
